//
//  countDown.m
//  代码布局
//
//  Created by iOS on 16/8/2.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "countDown.h"
@interface countDown()
@property (nonatomic,assign)CGSize size;
@end
@implementation countDown
static int hour ;
static int days ;
static int minute ;
static int second ;

- (instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)setSomeTime:(NSString *)someTime{
    _someTime = someTime;
    [self timer];
}

- (void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSString * daysString = [NSString stringWithFormat:@"%d天",days];
    CGSize sizeDay = [self getstring:daysString AndFont:self.font];
    
    /*写文字*/
    UIFont  *font = self.font;//定义默认字体
    CGContextSetRGBFillColor (context,  1, 0, 0, 1.0);//设置填充颜色:红色
    [daysString drawInRect:CGRectMake(0, 1, sizeDay.width, sizeDay.height) withFont:font];
    
#pragma mark 小时
   
    NSString * hourString = [NSString stringWithFormat:@"%.2d ",hour];
    CGSize sizeHour = [self getstring:hourString AndFont:self.font];
    
    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
    CGContextSetLineWidth(context, 1.0);
    CGContextAddRect(context, CGRectMake(sizeDay.width + 2 , 0, sizeHour.width + 2 , sizeHour.height));
    CGContextStrokePath(context);
    
    CGContextSetRGBFillColor (context,  1, 0, 0, 1.0);//设置填充颜色:红色
    [hourString drawInRect:CGRectMake(sizeDay.width + 3, 1, sizeHour.width , sizeHour.height) withFont:font];
    
#pragma mark 画冒号
   // NSString * mao = [NSString stringWithFormat:@":",hour];
    CGSize sizeMao = [self getstring:@":" AndFont:self.font];
    CGContextSetRGBFillColor (context,  1, 0, 0, 1.0);//设置填充颜色:红色
    [@":" drawInRect:CGRectMake(sizeDay.width + 3 +sizeHour.width + 5, 1, sizeMao.width , sizeMao.height) withFont:font];
    
#pragma mark 分钟
    NSString * minString = [NSString stringWithFormat:@"%.2d ",minute];
    CGSize sizeMin = [self getstring:minString AndFont:self.font];
    
    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
    CGContextSetLineWidth(context, 1.0);
    CGContextAddRect(context, CGRectMake(sizeDay.width + 3 +sizeHour.width + 5 + 8 , 0, sizeMin.width + 2 , sizeMin.height));
    CGContextStrokePath(context);
    
    CGContextSetRGBFillColor (context,  1, 0, 0, 1.0);//设置填充颜色:红色
    [minString drawInRect:CGRectMake(sizeDay.width + 3 +sizeHour.width + 5 + 12, 1, sizeMin.width , sizeMin.height) withFont:font];
    
#pragma mark 冒号2
    CGSize sizeMao2 = [self getstring:@":" AndFont:self.font];
    CGContextSetRGBFillColor (context,  1, 0, 0, 1.0);//设置填充颜色:红色
    [@":" drawInRect:CGRectMake(sizeDay.width + 3 +sizeHour.width + 5 + 8 + sizeMin.width + 5, 1, sizeMao2.width , sizeMao2.height) withFont:font];

#pragma mark 秒
    NSString * senString = [NSString stringWithFormat:@"%.2d ",second];
    CGSize sizeSen = [self getstring:senString AndFont:self.font];
    
    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
    CGContextSetLineWidth(context, 1.0);
    CGContextAddRect(context, CGRectMake(sizeDay.width + 3 +sizeHour.width + 5 + 8 + sizeMin.width + 5 + sizeMao2.width + 5 , 0, sizeSen.width + 2 , sizeSen.height));
    CGContextStrokePath(context);
    
    CGContextSetRGBFillColor (context,  1, 0, 0, 1.0);//设置填充颜色:红色
    [senString drawInRect:CGRectMake(sizeDay.width + 3 +sizeHour.width + 5 + 8 + sizeMin.width + 5 + sizeMao2.width + 5  +3, 1, sizeSen.width , sizeSen.height) withFont:font];

//    CGRect frame = self.frame;
//    frame.size.width = sizeDay.width + 3 +sizeHour.width + 5 + 8 + sizeMin.width + 5 + sizeMao2.width + 5  +3  + sizeSen.width - 20;
//    frame.size.height = sizeSen.height;
//    self.frame = frame;
    self.size = CGSizeMake(sizeDay.width + 3 +sizeHour.width + 5 + 8 + sizeMin.width + 5 + sizeMao2.width + 5 + sizeSen.width+4, sizeSen.height+2);
//    self.frame.size = self.size;
    [self.delegate fntSize:self.size];
}
//- (CGSize)fntSize{
//    return self.size;
//}
-(CGSize)getstring:(NSString*)string AndFont:(UIFont*)font{
    NSDictionary *attrs1 = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:fontNunber(14)]};
    CGSize size1 = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs1 context:nil].size;
    return size1;
}


- (void)timer{
    if(!self.time){
    self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(click) userInfo:nil repeats:YES];
    }
   // [time invalidate];
}
- (void)click{
    [self timecountdown:self.someTime];
  //  NSLog(@"EEEEEEEE");
}
- (void)timecountdown:(NSString*)someTime{
    NSString *timeString=@"";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    
    
    NSInteger timeLine = [someTime integerValue];
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeLine];
    
    NSTimeInterval cha1 = [time timeIntervalSinceDate: datenow];
    // int allhour = (int)(cha/3600) - 15*3600 - 14*60;
    // NSTimeInterval cha = cha1 - 15*3600 - 14*60 + 12;
    NSTimeInterval cha = cha1;
    int allhour = (int)cha/3600;
    hour = allhour%24;
    days = allhour/24;
    minute = (int)(cha - allhour*3600)/60;
    second = cha - allhour*3600 - minute*60;
    [self setNeedsDisplay];
}
- (void)dealloc{
    
    if (self.time) {
        [self.time invalidate];
        self.time = nil;
    }
}
@end
